<template>
	<view>
		<view class="wanl-product">
			<view class="product_warter" :class="dataStyle">
				<view class="wanl-waterfall">
					<view id="wanl-left-cloumn" class="wanl-cloumn">
						<view class="warters left" style="border-radius: 6rpx !important;" v-for="(item, index) in leftList" :key="index"
							@tap="handleGoods(item.id)">
							<view class="img-wrap">
								<image style="height: 180rpx;" class="image" :src="$wanlshop.oss(item.image)" mode="aspectFill"></image>
							</view>
							<view class="content">
								<view class="text-cut-2 title">
									<text>{{ item.title }}</text>
								</view>
								<view class="address flex justify-start align-center margin-top-xs">
									<view class="flex justify-start align-center" style="color:#888888;">
										<text class="icon-address iconfont" style="font-size: 22rpx;"></text>
										<text class="text-x">全{{item.citys}}·可服务</text>
									</view>
									<!-- <view class="flex justify-start align-center margin-left-xs" style="color: #0E50FC;">
										<text class="icon-renzhengyonghu iconfont" style="font-size: 22rpx;"></text>
										<text class="text-x">企业认证</text>
									</view> -->
								</view>
								<view class="cont flex justify-between align-center">
									<view class="left">
										<view class="flex justify-start align-center margin-top-sm">
											<uni-rate readonly :max="5" :value="item.score_text" :size="12" color="#CCCCCC" active-color="#4CBD66"/>
											<text style="color: #AAAAAA;" class="text-x margin-left-xs">{{item.comment}}条评价</text>
										</view>
										<view class="pingjiang margin-top-sm" v-if="item.slogan">
											“{{item.slogan}}”
										</view>
									</view>
								</view>
								
								<view class="tag">
									<text v-for="(d,i) in item.tags_text" :key="i">{{d}}</text>
								</view>
							</view>
						</view>
					</view>
					<view id="wanl-right-cloumn" class="wanl-cloumn">
						<view class="warters right" style="border-radius: 6rpx !important;" v-for="(item, index) in rightList" :key="index" @tap="handleGoods(item.id)">
							<view class="img-wrap">
								<image style="height: 180rpx;" class="image" :src="$wanlshop.oss(item.image)" mode="aspectFill"></image>
							</view>
							<view class="content">
								<view class="text-cut-2 title">
									<text>{{ item.title }}</text>
								</view>
								<view class="address flex justify-start align-center margin-top-xs">
									<view class="flex justify-start align-center" style="color:#888888;">
										<text class="icon-address iconfont" style="font-size: 22rpx;"></text>
										<text class="text-x margin-left-xs">全合肥·可服务</text>
									</view>
									<!-- <view class="flex justify-start align-center margin-left-xs" style="color: #0E50FC;">
										<text class="icon-renzhengyonghu iconfont" style="font-size: 22rpx;"></text>
										<text class="text-x margin-left-xs">企业认证</text>
									</view> -->
								</view>
								
								<view class="cont flex justify-between align-center">
									<view class="left">
										<view class="flex justify-start align-center margin-top-sm">
											<uni-rate readonly :max="5" :value="item.score_text" :size="12" color="#CCCCCC" active-color="#4CBD66"/>
											<text style="color: #AAAAAA;" class="text-x margin-left-xs">{{item.comment}}条评价</text>
										</view>
										<view class="pingjiang margin-top-sm" v-if="item.slogan">
											“{{item.slogan}}”
										</view>
									</view>
								</view>
								
								<view class="tag">
									<text v-for="(d,i) in item.tags_text" :key="i">{{d}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	/**
	 * wanlHomeMaking 家政
	 * @property {Array} dataList 列表数据
	 * @property {String} dataStyle 列表样式
	 * @example <wanl-product />
	 */
	export default {
		name: 'wanlHomeMaking',
		props: {
			dataList: {
				type: Array,
				required: true,
				default: function() {
					return [];
				}
			},
			dataStyle: {
				type: String,
				default: 'col-1-10'
			},
			dataType: {
				type: String,
				default: 'goods'
			}
		},
		data() {
			return {
				addTime: 180,
				leftList: [],
				rightList: [],
				tempList: [],
				children: []
			}
		},
		watch: {
			copyFlowList(nVal, oVal) {
				// 取差值，即这一次数组变化新增的部分
				let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0;
				if (nVal.slice(startIndex).length == 0) {
					this.leftList = [];
					this.rightList = [];
					this.tempList = this.cloneData(this.copyFlowList);
				}else{
					this.tempList = this.tempList.concat(this.cloneData(nVal.slice(startIndex)));
				}
				this.splitData();
			},
		},
		mounted() {
			this.tempList = this.cloneData(this.copyFlowList);
			this.splitData();
		},
		computed: {
			// 破坏flowList变量的引用，否则watch的结果新旧值是一样的
			copyFlowList() {
				return this.cloneData(this.dataList);
			}
		},
		methods: {
			async splitData() {
				if (!this.tempList.length) return;
				let rightRect = await this.getRect('#wanl-right-cloumn');
				let leftRect = await this.getRect('#wanl-left-cloumn');
				// 如果左边小于或等于右边，就添加到左边，否则添加到右边
				let item = this.tempList[0];
				if(!item) return ;
				item.image = this.$wanlshop.oss(item.image, 172, 0, 1);
				item.comment = this.$wanlshop.toFormat(item.comment, 'hundred');
				item.praise = item.comment > 0 ? parseInt((item.praise / item.comment) * 100) : 0;
				if (leftRect.height < rightRect.height) {
					this.leftList.push(item);
				} else if (leftRect.height > rightRect.height) {
					this.rightList.push(item);
				} else {
					// 这里是为了保证第一和第二张添加时，左右都能有内容
					// 因为添加第一张，实际队列的高度可能还是0，这时需要根据队列元素长度判断下一个该放哪边
					if (this.leftList.length <= this.rightList.length) {
						this.leftList.push(item);
					} else {
						this.rightList.push(item);
					}
				}
				// 移除临时列表的第一项
				this.tempList.splice(0, 1);
				// 如果临时数组还有数据，继续循环
				if (this.tempList.length) {
					setTimeout(() => {
						this.splitData();
					}, this.addTime)
				}
			},
			// 复制而不是引用对象和数组
			cloneData(data) {
				return JSON.parse(JSON.stringify(data));
			},
			getRect(selector, all) {
				return new Promise(resolve => {
					let query = null;
					// 支付宝小程序不能加后面的.in(this)，是它自身的限制
					// #ifndef MP-ALIPAY
					query = uni.createSelectorQuery().in(this)
					// #endif
					// #ifdef MP-ALIPAY
					query = uni.createSelectorQuery()
					// #endif
					query[all ? 'selectAll' : 'select'](selector)
						.boundingClientRect(rect => {
							if (all && Array.isArray(rect) && rect.length) {
								resolve(rect)
							}
							if (!all && rect) {
								resolve(rect)
							}
						})
						.exec()
				})
			},
			handleGoods(id) {
				this.$wanlshop.to(`/pages/medicalcare/homemaking/product?id=${id}`);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.wanl-waterfall {
		display: flex;
		flex-direction: row;
		align-items: flex-start;
		.wanl-cloumn {
			display: flex;
			flex: 1;
			flex-direction: column;
			height: auto;
			width: 50%;
			.wanl-image {
				width: 100%;
			}
		}
	}
	.water {
		border-radius: 6rpx !important;
	}
	.wanl-product .product_list.col-1-10 {
		margin: 0 30rpx;
	}
	.wanl-product .product_list.col-1-10 .item {
		margin-bottom: 0;
		border-bottom: 1rpx solid rgba(182,182,182,0.2);
		border-radius: 0;
	}
	.content {
		position: relative;
		padding: 20rpx 14rpx 10rpx 14rpx;
		.address {
			position: relative;
			z-index: 99;
			view {
				text {
					&:nth-child(1) {
						margin-top: 2rpx;
					} 
					&:nth-child(2) {
						margin-left: 4rpx;
					}
				}
			}
		}
		.title {
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: 600;
			color: #333333;
			font-size: 28rpx;
			position: relative;
			z-index: 99;
		}
		.tag {
			position: relative;
			margin-top: 10rpx;
			z-index: 99;
			text {
				margin-bottom: 10rpx;
				display: inline-block;
				font-size: 20rpx;
				color: #555555;
				// width: 102rpx;
				padding: 0 6rpx;
				height: 36rpx;
				border-radius: 4rpx;
				border: 1rpx solid #B6B6B6;
				text-align: center;
				line-height: 36rpx;
				margin-right: 10rpx;
				&:last-child {
					margin-right: 0;
				}
			}
		}
		.pingjiang {
			color: #4CBD66;
			font-size: 20rpx;
			background-color: #E6FFDB;
			border-radius: 6rpx;
			padding: 8rpx;
			display: inline-block;
			position: relative;
			z-index: 99;
		}
		.cont {
			
			> image {
				position: absolute;
				width: 160rpx;
				right: -20rpx;
				bottom: 55rpx;
			}
		}
	}
	.wanl-product .product_warter.col-2-20 .warters.left {
	    margin-right: 5px;
	}
	.wanl-product .product_warter.col-2-20 .warters.right {
	    margin-left: 5px;
	}
	.wanl-product .product_warter.col-2-20 .warters {
	    margin-bottom: 10px;
		> .img-wrap {
			.image {
				border-radius: 6rpx 6rpx 0 0;
			}
		}
	}
	.wanl-product .product_warter .warters {
	    border-radius: 6rpx !important;
	    background-color: #ffffff;
	    overflow: hidden;
	}
</style>
